package com.itheima.d5_tcp2;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class gaizao {
    public static void main(String[] args) throws Exception {
        // 1、创建Socket对象，并同时请求与服务端程序的连接。

        new gaizao();

        Socket socket = new Socket("127.0.0.1", 10014);

        // 2、从socket通信管道中得到一个字节输出流，用来发数据给服务端程序。
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        // 3、把低级的字节输出流包装成高效的输出流
        BufferedWriter bw = new BufferedWriter(osw);
        // 4、开始写数据出去了
        bw.write("嘿哈哈嘿");
        bw.newLine();
        bw.flush();
        // 5、释放连接资源
        bw.close();
        socket.close();
    }
}

class clientThread implements Runnable{
    private String s;
    public clientThread(String s){
        this.s = s;
    }

    @Override
    public void run() {
        Socket socket = null;
        BufferedWriter bw = null;
        try {
            socket = new Socket(s, 10086);

            // 2、从socket通信管道中得到一个字节输出流，用来发数据给服务端程序。
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            // 3、把低级的字节输出流包装成高效的输出流
            bw = new BufferedWriter(osw);
            // 4、开始写数据出去了
            bw.write("嘿哈哈嘿");
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
           e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 5、释放连接资源

    }
}
